<template>
  <div class="agent-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>智能体管理</span>
          <el-button type="primary" @click="showRegisterDialog">注册智能体</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm">
        <el-form-item label="类型">
          <el-input v-model="queryForm.agentType" placeholder="智能体类型" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="状态" clearable>
            <el-option label="在线" value="ONLINE" />
            <el-option label="离线" value="OFFLINE" />
            <el-option label="忙碌" value="BUSY" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadData">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="agentId" label="智能体ID" width="200" />
        <el-table-column prop="agentName" label="名称" width="150" />
        <el-table-column prop="agentType" label="类型" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="endpoint" label="端点" />
        <el-table-column prop="heartbeatTime" label="最后心跳" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="viewDetail(row)">详情</el-button>
            <el-button size="small" type="danger" @click="handleUnregister(row)">注销</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="pagination.current"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        @current-change="loadData"
        layout="total, prev, pager, next"
        style="margin-top: 20px; justify-content: center"
      />
    </el-card>

    <el-dialog v-model="registerDialogVisible" title="注册智能体" width="500px">
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" label-width="100px">
        <el-form-item label="名称" prop="agentName">
          <el-input v-model="registerForm.agentName" />
        </el-form-item>
        <el-form-item label="类型" prop="agentType">
          <el-input v-model="registerForm.agentType" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="registerForm.description" type="textarea" />
        </el-form-item>
        <el-form-item label="能力">
          <el-input v-model="registerForm.capabilities" placeholder='["能力1", "能力2"]' />
        </el-form-item>
        <el-form-item label="端点" prop="endpoint">
          <el-input v-model="registerForm.endpoint" placeholder="http://localhost:8001" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="registerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleRegister">确定</el-button>
      </template>
    </el-dialog>

    <el-dialog v-model="detailDialogVisible" title="智能体详情" width="600px">
      <el-descriptions :column="1" border v-if="currentAgent">
        <el-descriptions-item label="智能体ID">{{ currentAgent.agentId }}</el-descriptions-item>
        <el-descriptions-item label="名称">{{ currentAgent.agentName }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ currentAgent.agentType }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusType(currentAgent.status)">{{ currentAgent.status }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="描述">{{ currentAgent.description }}</el-descriptions-item>
        <el-descriptions-item label="能力">{{ currentAgent.capabilities }}</el-descriptions-item>
        <el-descriptions-item label="端点">{{ currentAgent.endpoint }}</el-descriptions-item>
        <el-descriptions-item label="最后心跳">{{ currentAgent.heartbeatTime }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ currentAgent.createTime }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAgentList, registerAgent, unregisterAgent, getAgentDetail } from '@/api/agent'

const tableData = ref([])
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

const queryForm = reactive({
  agentType: '',
  status: ''
})

const registerDialogVisible = ref(false)
const detailDialogVisible = ref(false)
const currentAgent = ref(null)

const registerForm = reactive({
  agentName: '',
  agentType: '',
  description: '',
  capabilities: '',
  endpoint: ''
})

const registerRules = {
  agentName: [{ required: true, message: '请输入智能体名称', trigger: 'blur' }],
  agentType: [{ required: true, message: '请输入智能体类型', trigger: 'blur' }],
  endpoint: [{ required: true, message: '请输入访问端点', trigger: 'blur' }]
}

const registerFormRef = ref(null)

const loadData = async () => {
  const params = {
    current: pagination.current,
    size: pagination.size,
    ...queryForm
  }
  const res = await getAgentList(params)
  tableData.value = res.data.records
  pagination.total = res.data.total
}

const resetQuery = () => {
  queryForm.agentType = ''
  queryForm.status = ''
  pagination.current = 1
  loadData()
}

const showRegisterDialog = () => {
  registerDialogVisible.value = true
}

const handleRegister = async () => {
  await registerFormRef.value.validate()
  await registerAgent(registerForm)
  ElMessage.success('注册成功')
  registerDialogVisible.value = false
  Object.assign(registerForm, {
    agentName: '',
    agentType: '',
    description: '',
    capabilities: '',
    endpoint: ''
  })
  loadData()
}

const handleUnregister = async (row) => {
  await ElMessageBox.confirm('确定要注销该智能体吗？', '提示', {
    type: 'warning'
  })
  await unregisterAgent(row.agentId)
  ElMessage.success('注销成功')
  loadData()
}

const viewDetail = async (row) => {
  const res = await getAgentDetail(row.agentId)
  currentAgent.value = res.data
  detailDialogVisible.value = true
}

const getStatusType = (status) => {
  const map = {
    ONLINE: 'success',
    OFFLINE: 'info',
    BUSY: 'warning'
  }
  return map[status] || 'info'
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.agent-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
